//
//  LGSportsEvaRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/11.
//  运动评估记录

#import "LGSportsEvaRecordController.h"
//填写评估数据
#import "LGSportsEvaInputController.h"
//运动评估详情
#import "LGSportsEvaDetailController.h"
#import "LGSportsEvaRecordCell.h"
#import "LGRecoverySectionView.h"
@interface LGSportsEvaRecordController ()<UITableViewDelegate,UITableViewDataSource>

/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGSportsEvaRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"运动评估";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, viewPix(95), Screen_W, Screen_H-topBarHeight-viewPix(95))];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.estimate/reports" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动评估" offset:0];
            [self.tableView bringSubviewToFront:self.emptyView];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动评估" offset:0];
            [self.tableView bringSubviewToFront:self.emptyView];
        }
    }];
}



//填写评估数据
-(void)inputSportEvaData{
    LGSportsEvaInputController *controller = [[LGSportsEvaInputController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.saveDataSuccess = ^{
        [weakSelf requestData];
    };
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportsEvaRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dateStr = self.dataArray[indexPath.row];
    cell.index = indexPath.row;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportsEvaDetailController *controller = [[LGSportsEvaDetailController alloc]init];
    controller.patientid = self.patientid;
    controller.dateStr = LGNSString(self.dataArray[indexPath.row]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGRecoverySectionView *headView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    headView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    headView.title = @"评估数据";
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

#pragma mark
#pragma mark ====> 创建控件

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSportsEvaRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(69);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(93))];
    baseView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    UIButton *button = [UIButton buttonWithTitle:@"填写评估数据" titleFont:LGFont(16) textColor:@"#333333" imageName:@""];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.frame = CGRectMake(viewPix(16), viewPix(20), Screen_W-viewPix(32), viewPix(64));
    button.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(21), 0, 0);
    [button setShadoWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(inputSportEvaData) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:button];
    UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    tipView.frame = CGRectMake(Screen_W-viewPix(50), viewPix(26), viewPix(12), viewPix(12));
    [button addSubview:tipView];
    return baseView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
